package com.xczl.book.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.shishuo.cms.entity.vo.PageVo;
import com.xczl.book.dao.BookCateDao;
import com.xczl.book.dao.BookDao;
import com.xczl.book.entity.Book;
import com.xczl.book.entity.BookCate;

@Service
@Transactional
public class BookService {

	@Autowired
	private BookDao bookDao;
	@Autowired
	private BookCateDao bookCateDao;

	/**
	 * 添加图书
	 * 
	 * @param book
	 */
	public void add(Book book) {
		// 实体是否存在
		Assert.notNull(book);
		Assert.notNull(book.getCateId(), "请选择图书分类");
		Assert.notNull(book.getNumber(), "请填写图书编号");
		// Assert.hasText(book.getUrlPic(), "请上传图片");
		Assert.hasText(book.getName(), "请填写书名");
		Assert.hasText(book.getAuthor(), "请填写作者");
		Assert.hasText(book.getBrief(), "请填写简介");
		Assert.hasText(book.getPress(), "请填写出版社");
		Assert.notNull(book.getTotalQuantity(), "请填写总数量");

		BookCate cate = bookCateDao.findById(book.getCateId());
		Assert.notNull(cate, "此分类不存在");

		book.setLibrarieNumber(book.getTotalQuantity());
		// Dao插入数据
		bookDao.insertBook(book);
	}

	/**
	 * 修改图书
	 * 
	 * @param book
	 */
	public void update(Book book) {
		// 实体是否存在
		Assert.notNull(book);
		Assert.notNull(book.getCateId(), "请选择图书分类");
		Assert.notNull(book.getNumber(), "请填写图书编号");
		Assert.hasText(book.getUrlPic(), "请上传图片");
		Assert.hasText(book.getName(), "请填写书名");
		Assert.hasText(book.getAuthor(), "请填写作者");
		Assert.hasText(book.getBrief(), "请填写简介");
		Assert.hasText(book.getPress(), "请填写出版社");
		bookDao.updateById(book);
	}

	/**
	 * 根据id查询
	 * 
	 * @return
	 */
	public Book findById(int id) {
		return bookDao.findById(id);
	}

	
	/**
	 * 根据编号查询
	 * 
	 * @return
	 */
	public Book findByNumber(String number) {
		return bookDao.searchByNumber(number);
	}
	
	
	/**
	 * 根据id查全部
	 * 
	 * @return
	 */
	public PageVo<Book> findPageVo(int pageNum,Book searchVo) {
		PageVo<Book> pageVo = new PageVo<Book>(pageNum);//创建翻页对象
		pageVo.setRows(10);
		List<Book> list = bookDao.findPageVoBySearch(searchVo,pageVo.getOffset(),
				pageVo.getRows());//翻页查询
		int count = bookDao.findCountBySearch(searchVo);//查询总记录数
		
		pageVo.setList(list);//设置内容
		pageVo.setCount(count);//设置总记录数
		return pageVo;
	}

	/**
	 * 修改数量
	 * @param id
	 * @param quantity
	 */
	public void editLibrarieNumber(int id, int quantity) {
		//根据ID查询书籍对象
		Assert.isTrue(quantity!=0,"数量不能输0");
		bookDao.updateLibrarieNumber(id, quantity);
		//根据quantity设置在馆数量和总数量
		//保存对象
	}
	
	/**
	 * 修改数量
	 * @param id
	 * @param quantity
	 */
	public void editQuantity(int id, int quantity) {
		//根据ID查询书籍对象
		Assert.isTrue(quantity!=0,"数量不能输0");
		bookDao.updateByquantity(id, quantity);
		//根据quantity设置在馆数量和总数量
		//保存对象
	}
}
